package com.scott.excel.read;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.util.Map;
 
public class BigExcelReader4GExport {
	public static String gen4Gcsv(String filepath) throws IOException {
		// String filepath = "C:/luo/doc/Neteq_HN/小区场景/4G-source-demo.xlsx";
		FileWriter writer = null;
		String path = ExportUtil.getEXPORT_PATH() + "4G_" + (new Date()).getTime() + ".csv";
		System.out.println("新生成的文件名: " + path);
		writer = new FileWriter(path);
		final BufferedWriter bw = new BufferedWriter(writer);;
		try {
			// 写入标题
			bw.write("小区标识(十进制),TAC(十进制),经度,纬度,天线方向角,基站制式,城市 ,小区,小区名称,位置,小区类型,基站标识,基站类型,基站配置,天线高度,机械下倾角,电子下倾角,BSC/RNC名称,下行频点,扰码,导频功率,邻站信息,eNBId(十进制),ECI(十进制),eNBId(十六进制),小区标识(十六进制),ECI(十六进制)");
			
			BigExcelReader reader = new BigExcelReader(filepath) {
				@Override
				protected void outputRow(String[] datas, int[] rowTypes, int rowIndex) {
					if(rowIndex > 0) {
						String line = getLine(datas);
						
						if(line != null) {
							try {
								System.out.println(line);
								bw.write(line);
							} catch (IOException e) {
								e.printStackTrace();
							}
							if(rowIndex % 10000 == 0) {
								try {
									bw.flush();
								} catch (IOException e) {
									e.printStackTrace();
								}
							}
						}
					}
				}
			};
			// 执行解析
			reader.parse();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(bw != null) {
				try {
					bw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return path;
	}
	
	private static Map<String, Integer> IDX_MAPPING = ExportUtil.getIDX_MAPPING();
	private static StringBuffer sb = new StringBuffer();
	public static String getLine(String datas[]) {
		try {
			sb.setLength(0);
			// 此处输出每一行的数据
			
			// dataMap.put("小区标识(十进制)", IDX_MAPPING.get("L"));
			setLine(datas[IDX_MAPPING.get("I")]);
			// dataMap.put("TAC(十进制)", IDX_MAPPING.get(""));
			setLine("0");
			// dataMap.put("经度", IDX_MAPPING.get("O"));
			setLine(datas[IDX_MAPPING.get("O")]);
			// dataMap.put("纬度", IDX_MAPPING.get("P"));
			setLine(datas[IDX_MAPPING.get("P")]);
			// dataMap.put("天线方向角", IDX_MAPPING.get("0"));
			setLine("0");
			// dataMap.put("基站制式", IDX_MAPPING.get("FDD"));
			setLine("4G");
			// dataMap.put("城市", IDX_MAPPING.get("E"));
			setLine(datas[IDX_MAPPING.get("E")]);
			// dataMap.put("小区", IDX_MAPPING.get("C"));
			setLine(datas[IDX_MAPPING.get("C")]);
			// dataMap.put("小区名称", IDX_MAPPING.get("C"));
			setLine(datas[IDX_MAPPING.get("C")]);
			// dataMap.put("位置", IDX_MAPPING.get("F"));
			setLine(datas[IDX_MAPPING.get("C")]);
			// dataMap.put("小区类型", IDX_MAPPING.get("室外"));
			setLine("室外");
			// dataMap.put("基站标识", IDX_MAPPING.get("H"));
			setLine(datas[IDX_MAPPING.get("H")]);
			// dataMap.put("基站类型", IDX_MAPPING.get("宏站"));
			setLine("宏站");
			// dataMap.put("基站配置", IDX_MAPPING.get(""));
			setLine("");
			// dataMap.put("天线高度", IDX_MAPPING.get("0"));
			setLine("0");
			// dataMap.put("机械下倾角", IDX_MAPPING.get("0"));
			setLine("0");
			// dataMap.put("电子下倾角", IDX_MAPPING.get("0"));
			setLine("0");
			// dataMap.put("BSC/RNC名称", IDX_MAPPING.get(""));
			setLine("");
			// dataMap.put("下行频点", IDX_MAPPING.get("Y"));
			setLine(datas[IDX_MAPPING.get("Y")]);
			// dataMap.put("扰码", IDX_MAPPING.get("0"));
			setLine("0");
			// dataMap.put("导频功率", IDX_MAPPING.get("AB"));
			setLine(datas[IDX_MAPPING.get("AB")]);
			// dataMap.put("邻站信息", IDX_MAPPING.get(""));
			setLine("");
			// dataMap.put("eNBId(十进制)", IDX_MAPPING.get("H"));
			setLine(datas[IDX_MAPPING.get("H")]);
			
			String h = Integer.toHexString(Integer.parseInt(datas[IDX_MAPPING.get("H")])).toUpperCase();
			String i = Integer.toHexString(Integer.parseInt(datas[IDX_MAPPING.get("I")])).toUpperCase();
			String hi = h + i;
			String eci = Integer.parseInt(hi, 16) + "";
			
			// dataMap.put("ECI(十进制)", IDX_MAPPING.get("ddddddd"));
			setLine(eci);
			// dataMap.put("eNBId(十六进制)", IDX_MAPPING.get("eNBId 转换"));
			setLine(h);
			// dataMap.put("小区标识(十六进制)", IDX_MAPPING.get("小区标识 转换"));
			setLine(i);
			// dataMap.put("ECI(十六进制)", IDX_MAPPING.get("ECI 转换"));
			setLine(hi);
			
			sb.setLength(sb.length() - 1);
			
			sb.append("\n");
			
			return sb.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static void setLine(String val) {
		sb.append(val).append(",");
	}
}
